package com.cinc.ebc.fi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cinc.ebc.fi.constant.AparOtherStatus;
import com.cinc.ebc.fi.constant.AparVoucherStatus;
import com.cinc.ebc.fi.entity.FiOtherPayment;
import com.cinc.ebc.fi.entity.FiOtherPaymentDetail;
import com.cinc.ebc.fi.mapper.FiOtherPaymentDetailMapper;
import com.cinc.ebc.fi.mapper.FiOtherPaymentMapper;
import com.cinc.ebc.fi.service.IFiOtherPaymentService;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @Description: 其他应付单
 * @Author: jeecg-boot
 * @Date:   2022-12-25
 * @Version: V1.0
 */
@Service
public class FiOtherPaymentServiceImpl extends ServiceImpl<FiOtherPaymentMapper, FiOtherPayment> implements IFiOtherPaymentService {

	@Autowired
	private FiOtherPaymentMapper fiOtherPaymentMapper;
	@Autowired
	private FiOtherPaymentDetailMapper fiOtherPaymentDetailMapper;

	@Override
	@Transactional
	public void saveMain(FiOtherPayment fiOtherPayment, List<FiOtherPaymentDetail> fiOtherPaymentDetailList) {
		BigDecimal paymentAmount = fiOtherPayment.getPaymentAmount();
		if(paymentAmount == null || paymentAmount.compareTo(BigDecimal.ZERO) != 1){
			throw new RuntimeException("应付金额必须大于零");
		}
		fiOtherPaymentMapper.insert(fiOtherPayment);
		if(fiOtherPaymentDetailList!=null && fiOtherPaymentDetailList.size()>0) {
			for(FiOtherPaymentDetail entity:fiOtherPaymentDetailList) {
				BigDecimal detailPaymentAmount = entity.getPaymentAmount();
				if(detailPaymentAmount == null || detailPaymentAmount.compareTo(BigDecimal.ZERO) != 1){
					throw new RuntimeException("应付金额必须大于零");
				}
				//外键设置
				entity.setMainId(fiOtherPayment.getId());
				fiOtherPaymentDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(FiOtherPayment fiOtherPayment,List<FiOtherPaymentDetail> fiOtherPaymentDetailList) {
		FiOtherPayment fiOtherPaymentHis = fiOtherPaymentMapper.selectById(fiOtherPayment.getId());
		if(!AparOtherStatus.STATUS_01.equals(fiOtherPaymentHis.getStatus())){
			throw new RuntimeException("已经审核通过，不能修改");
		}
		BigDecimal paymentAmount = fiOtherPayment.getPaymentAmount();
		if(paymentAmount == null || paymentAmount.compareTo(BigDecimal.ZERO) != 1){
			throw new RuntimeException("应付金额必须大于零");
		}
		fiOtherPaymentMapper.updateById(fiOtherPayment);

		//1.先删除子表数据
		fiOtherPaymentDetailMapper.deleteByMainId(fiOtherPayment.getId());

		//2.子表数据重新插入
		if(fiOtherPaymentDetailList!=null && fiOtherPaymentDetailList.size()>0) {
			for(FiOtherPaymentDetail entity:fiOtherPaymentDetailList) {
				BigDecimal detailPaymentAmount = entity.getPaymentAmount();
				if(detailPaymentAmount == null || detailPaymentAmount.compareTo(BigDecimal.ZERO) != 1){
					throw new RuntimeException("应付金额必须大于零");
				}
				//外键设置
				entity.setMainId(fiOtherPayment.getId());
				fiOtherPaymentDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		FiOtherPayment fiOtherPaymentHis = fiOtherPaymentMapper.selectById(id);
		if(!AparOtherStatus.STATUS_01.equals(fiOtherPaymentHis.getStatus())){
			throw new RuntimeException("已经审核通过，不能删除");
		}
		fiOtherPaymentDetailMapper.deleteByMainId(id);
		fiOtherPaymentMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			delMain(id.toString());
		}
	}

	@Transactional
	@Override
	public void audit(List<FiOtherPayment> fiOtherPaymentList) {
		for(FiOtherPayment fiOtherPayment : fiOtherPaymentList) {
			FiOtherPayment fiOtherPaymentEntity = fiOtherPaymentMapper.selectById(fiOtherPayment.getId());
			if (fiOtherPaymentEntity == null) {
				throw new RuntimeException("未找到对应数据");
			}
			String status = fiOtherPaymentEntity.getStatus();
			if (!AparOtherStatus.STATUS_01.equals(status)) {
				throw new RuntimeException("已经审核，请不要重复操作");
			}
			BigDecimal paymentAmount = fiOtherPaymentEntity.getPaymentAmount();
			if (paymentAmount == null || paymentAmount.compareTo(BigDecimal.ZERO) != 1) {
				throw new RuntimeException("应付金额无效，不能审核");
			}
			// 审核
			FiOtherPayment fiOtherPaymentAudit = new FiOtherPayment();
			fiOtherPaymentAudit.setId(fiOtherPayment.getId());
			fiOtherPaymentAudit.setAuditDate(new Date());
			fiOtherPaymentAudit.setAuditFlag("1");
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			fiOtherPaymentAudit.setAuditor(sysUser != null ? sysUser.getUsername() : null);
			fiOtherPaymentAudit.setStatus(AparOtherStatus.STATUS_02);
			fiOtherPaymentMapper.updateById(fiOtherPaymentAudit);
		}
	}

}
